import { defineStore } from 'pinia'

const useSearchStore = defineStore('search', {
  state: () =>
    ({
      current: 1, // 当前页
      total: 3, //总页
      fontSize: 32, // 字体大小
      from: 'diys', // 来源: cards | diys // TODO
    }) as { current: number; total: number; fontSize: number; from: 'cards' | 'diys' },
  actions: {
    setCurrent(num: number) {
      if (num < 1) {
        this.current = 1
      } else if (num > this.total) {
        this.current = this.total
      } else {
        this.current = num
      }
    },
    add() {
      if (this.current < this.total) {
        this.current++
      }
    },
    subtract() {
      if (this.current > 1) {
        this.current--
      }
    },
    clear() {
      this.current = 1
    },
    changeFrom() {
      if (this.from === 'cards') {
        this.from = 'diys'
      } else {
        this.from = 'cards'
      }
    },
  },
})

export default useSearchStore
